package com.openbravo.pos.panels;

import javax.swing.JPanel;
import java.awt.FlowLayout;
import javax.swing.JButton;

import com.openbravo.pos.base.AppLocal;
import net.sourceforge.jtimepiece.TimeUtil;


import com.openbravo.pos.panels.event.TimeCycleChangedEvent;
import com.openbravo.pos.panels.event.TimeCycleChangedListener;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Date;
import javax.swing.JToggleButton;
import javax.swing.ButtonGroup;

public class QuickTimerFilter extends JPanel {
	 public static int TIMERCYCLETYPE_Null=-1;
     public static int TIMERCYCLETYPE_Today=0;
     public static int TIMERCYCLETYPE_Week=1;
     public static int TIMERCYCLETYPE_ThreeWeek=2;
     public static int TIMERCYCLETYPE_Month=3;
     public static int TIMERCYCLETYPE_Muarter=4;
     
     private int timeCycleType;
     private java.util.Date startDate;
     private java.util.Date endDate;
	/**
	 * Create the panel.
	 */
	private JToggleButton btnMuarter;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private JToggleButton bntToday;
	private JToggleButton btnWeek;
	private JToggleButton btnThreeWeek;
	private JToggleButton btnMonth;
	private JButton btnLastCycle;
	private JButton btnNextCycle;
	public QuickTimerFilter() {
		initComponents();
		timeCycleType=TIMERCYCLETYPE_Null;
	}
	
	private void initComponents() {
		setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
		
		 bntToday = new JToggleButton(AppLocal.getIntString("Button.Today"));
		 bntToday.addActionListener(new ActionListener() {
		 	public void actionPerformed(ActionEvent e) {
		 		fireTodayCycle();
		 	}
		 });
		buttonGroup.add(bntToday);
		add(bntToday);
		
		 btnWeek = new JToggleButton(AppLocal.getIntString("Button.CurrentWeek"));
		 btnWeek.addActionListener(new ActionListener() {
		 	public void actionPerformed(ActionEvent e) {
		 		fireWeekCycle();
		 	}
		 });
		buttonGroup.add(btnWeek);
		add(btnWeek);
		
		 btnThreeWeek = new JToggleButton(AppLocal.getIntString("Button.CurrentThreeWeek"));
		 btnThreeWeek.addActionListener(new ActionListener() {
		 	public void actionPerformed(ActionEvent e) {
		 		fireThreeWeekCycle();
		 	}
		 });
		buttonGroup.add(btnThreeWeek);
		add(btnThreeWeek);
		
		 btnMonth = new JToggleButton(AppLocal.getIntString("Button.CurrentMonth"));
		 btnMonth.addActionListener(new ActionListener() {
		 	public void actionPerformed(ActionEvent e) {
		 		fireMonthCycle();
		 	}
		 });
		buttonGroup.add(btnMonth);
		add(btnMonth);
		
		btnMuarter = new JToggleButton(AppLocal.getIntString("Button.CurrentMuarter"));
		btnMuarter.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				fireMuarterCycle();
			}
		});
		buttonGroup.add(btnMuarter);
		add(btnMuarter);
		
		 btnLastCycle = new JButton(AppLocal.getIntString("Button.LastCycle"));
		 btnLastCycle.addActionListener(new ActionListener() {
		 	public void actionPerformed(ActionEvent e) {
		 		fireLastCycle();
		 	}
		 });
		add(btnLastCycle);
		
		 btnNextCycle = new JButton(AppLocal.getIntString("Button.NextCycle"));
		 btnNextCycle.addActionListener(new ActionListener() {
		 	public void actionPerformed(ActionEvent e) {
		 		fireNextCycle();
		 	}
		 });
		add(btnNextCycle);
	}
	
    public void addTimeCycleChangedListener(TimeCycleChangedListener l){
    	listenerList.add(TimeCycleChangedListener.class, l);
    }
    
    public void removeTimeCycleChangedListener(TimeCycleChangedListener l){
    	listenerList.remove(TimeCycleChangedListener.class, l);
    }
    
    public void fireTimeCycleChanged(){
    	for(TimeCycleChangedListener l:listenerList.getListeners(TimeCycleChangedListener.class))
    		l.timeCycleChanged(new TimeCycleChangedEvent(this,timeCycleType,startDate, endDate));
    }

	public int getTimeCycleType() {
		return timeCycleType;
	}

	public void setTimeCycleType(int timeCycleType) {
		this.timeCycleType = timeCycleType;
	}

	public java.util.Date getStartDate() {
		return startDate;
	}

	public void setStartDate(java.util.Date startDate) {
		this.startDate = startDate;
	}

	public java.util.Date getEndDate() {
		return endDate;
	}

	public void setEndDate(java.util.Date endDate) {
		this.endDate = endDate;
	}
	
	public void resert(){
		timeCycleType=TIMERCYCLETYPE_Null;
		startDate=null;
		endDate=null;
		buttonGroup.clearSelection();
	}

	public void setDefineSelected(int type){
		if(type==TIMERCYCLETYPE_Null){
			resert();
		}
		if(type== TIMERCYCLETYPE_Today){
			  bntToday.setSelected(true);
			  fireTodayCycle();
		}if(type== TIMERCYCLETYPE_Week){
			  btnWeek.setSelected(true);
			  fireWeekCycle();
		}if(type== TIMERCYCLETYPE_ThreeWeek){
			  btnThreeWeek.setSelected(true);
			  fireThreeWeekCycle();
		}if(type==TIMERCYCLETYPE_Month){
		      btnMonth.setSelected(true);
		      fireMonthCycle();
		}if(type==TIMERCYCLETYPE_Muarter){
			  btnMuarter.setSelected(true);
			  fireMuarterCycle();
		}
	}
	
	public void fireTodayCycle() {
		startDate=TimeUtil.GetDateNoTime(new Date());
	    this.endDate=null;	
		timeCycleType=QuickTimerFilter.TIMERCYCLETYPE_Today;
		fireTimeCycleChanged();
	}


	public void fireWeekCycle() {
		startDate=TimeUtil.GetDateNoTime(new Date());
		startDate=TimeUtil.getFirstDateForWeekOfDate(startDate);
	    endDate=null;
		timeCycleType=QuickTimerFilter.TIMERCYCLETYPE_Week;
		fireTimeCycleChanged();
	}

	
	public void fireThreeWeekCycle() {
		
		startDate=TimeUtil.GetDateNoTime(new Date());	
		startDate=TimeUtil.getFirstDateForWeekOfDate(startDate);
		startDate=TimeUtil.addDay(startDate, -14);
		endDate=null;
		timeCycleType=QuickTimerFilter.TIMERCYCLETYPE_ThreeWeek;
		fireTimeCycleChanged();
	}


	public void fireMonthCycle() {
	    startDate=TimeUtil.GetDateNoTime(new Date());	
		startDate=TimeUtil.getFirstDayForMonthOfDate(startDate);
		endDate=null;	
		timeCycleType=QuickTimerFilter.TIMERCYCLETYPE_Month;
		fireTimeCycleChanged();
	}


	public void fireMuarterCycle() {
		
	}


	public void fireLastCycle() {
		if(startDate==null&&endDate==null)
			return;
		if(timeCycleType==QuickTimerFilter.TIMERCYCLETYPE_Null){	
			if(startDate!=null&&endDate!=null){
				int days=TimeUtil.getCalendarDaysAfter((Date)startDate, (Date)endDate);
				startDate=TimeUtil.addDay((Date)startDate, -days);
				endDate=TimeUtil.addDay((Date)endDate, -days);
			}else if(startDate!=null){
				int days=TimeUtil.getCalendarDaysAfter((Date)startDate, new Date());
				endDate=startDate;
				startDate=TimeUtil.addDay((Date)startDate, -days);
			}
		}else if(timeCycleType==QuickTimerFilter.TIMERCYCLETYPE_Today){
			int days=1;
			endDate=startDate;
			startDate=TimeUtil.addDay((Date)startDate, -days);
		}else if(timeCycleType==QuickTimerFilter.TIMERCYCLETYPE_Week){
			int days=7;
			endDate=startDate;
			startDate=TimeUtil.addDay((Date)startDate, -days);   
		}else if(timeCycleType==QuickTimerFilter.TIMERCYCLETYPE_ThreeWeek){
			int days=21;
			endDate=startDate;
			startDate=TimeUtil.addDay((Date)startDate, -days);   
		}else if(timeCycleType==QuickTimerFilter.TIMERCYCLETYPE_Month){
			endDate=startDate;
			startDate=TimeUtil.addMonth((Date)startDate, -1);
		}
		fireTimeCycleChanged();
	}

	public void fireNextCycle() {
		if(startDate==null&&endDate==null)
			return;
		if(startDate==null)
			startDate=TimeUtil.GetDateNoTime(new Date());
		if(endDate == null)
			endDate = startDate;
	    if(timeCycleType==QuickTimerFilter.TIMERCYCLETYPE_Null){	
	    	if(startDate!=null&&endDate!=null){
				int days=TimeUtil.getCalendarDaysAfter((Date)startDate, (Date)endDate);
				startDate=TimeUtil.addDay((Date)startDate, days);
				endDate=TimeUtil.addDay((Date)endDate, days);
			}else if(endDate!=null){
				int days=TimeUtil.getCalendarDaysAfter((Date)endDate, new Date());
				startDate=endDate;
				endDate=TimeUtil.addDay((Date)endDate, days);
			}
		}else if(timeCycleType==QuickTimerFilter.TIMERCYCLETYPE_Today){
			int days=1;
			startDate=endDate;
			endDate=TimeUtil.addDay((Date)endDate, days);
		}else if(timeCycleType==QuickTimerFilter.TIMERCYCLETYPE_Week){
			int days=7;
			startDate=endDate;
			endDate=TimeUtil.addDay((Date)endDate, days);   
		}else if(timeCycleType==QuickTimerFilter.TIMERCYCLETYPE_ThreeWeek){
			int days=21;
			startDate=endDate;
			endDate=TimeUtil.addDay((Date)endDate, days);   
		}else if(timeCycleType==QuickTimerFilter.TIMERCYCLETYPE_Month){
			startDate=endDate;
			endDate=TimeUtil.addMonth((Date)endDate, 1);
		}  
	    fireTimeCycleChanged();
	}
}
